import Notify from '@vant/weapp/notify/notify';

const app = getApp()
const util = require('../../utils/util.js');
const API = require('../../config/api.js');
import Service from '../../utils/pay';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Forms: {},
    seat: {},
    price: null,
    radio: 0,
    checkeds: [],
    title:'',
  },
  Submits(e) {
    util.request(API.OrderConfirm).then(res => {
      console.log(res)
      if (res.result === 'success') {
        if (res.data.state == 8) {
          wx.navigateTo({
            url: "/pages/mine/preorder"
          })
        } else if (res.data.state == 1) {
          //拉起支付
          var orderId = res.data.id;
          Service.payOrder(orderId)
            .then(res => {
              Service.payResult(res.paylogId);
            }, rej => {
              wx.navigateTo({
                url: "/pages/mine/preorder"
              })
            });
        }
      } else {
        console.log('error')
        Notify({
          type: 'warning',
          message: res.message
        })
      }
    }).catch(e => {
      Notify({
        type: 'warning',
        message: "服务器内部错误"
      })
    })
  },
  radioChange(e) {
    var order = app.globalData.recentOrder
    order.coupon_id = e.detail
    this.price()
  },
  price() {
    var order = app.globalData.recentOrder
    var self = this
    util.request(
      API.CalcPrice, {
        order_id: order.order_id ?? null,
        start_at: order.start_at,
        duration: order.duration,
        seat_id: order.seat_id,
        wallet_id: order.coupon_id
      }
    ).then(res => {
      if (res.result === 'success') {
        app.globalData.recentOrder = res.data
        var sta = res.data.start_at.split(' ')
        var eda = res.data.end_at.split(' ')
        var f = {
          date: sta[0],
          TimeStart: sta[1],
          TimeEnd: eda[1],
        }
        self.setData({
          Forms: f,
          title:res.data.title,
          price: res.data.amount,
          checkeds: res.data.coupon
        })
      }else{
          wx.navigateBack({
            success:()=>{
              wx.showToast({
                title: res.message,
                icon:'none',
              })
            }
          })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options',options);
    this.price()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})